<script setup lang="ts">
/**
 * 系统管理-系统设置-用户管理-编辑用户
 */
import {getRoleListApi} from "@/assets/api/systemManagement/system/role";
import {onMounted, ref} from "vue";
import {ElMessage, type FormInstance} from "element-plus";
import {editUserApi} from "@/assets/api/systemManagement/system/user";

const props = defineProps<{
  form: {
    name: string,
    account: string,
    password: string,
    deptId: number,
    status: number,
    remark: string,
    roleIds: string,
  },
  edit: Boolean,
  echoRoles: number[],
  deptList: { id: number, deptName: string }[]
}>();
const verifyPassword = ref('')
const emit = defineEmits(['save', 'close']);

// 获取角色列表
const roleList = ref();
const selectedRoles = ref(props.echoRoles);
const getRoleList = () => {
  getRoleListApi().then(res => {
    ElMessage.info(res.data.data)
    if (res.data.code == 200) {
      roleList.value = res.data.data;
    } else {
      ElMessage.error({message: res.data.data, showClose: true})
    }
  })
}

// 表单验证
const formRef = ref();
const roleValidator = (rule: any, value: string, callback: any) => {
  if (selectedRoles.value.length === 0) {
    callback(new Error('请选择角色'));
  } else {
    callback();
  }
}
const verifyPasswordValidator = (rule: any, value: string, callback: any) => {
  if (value !== props.form.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}
const rules = ref({
  name: [
    {required: true, message: '请输入姓名', trigger: 'blur'},
  ],
  account: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
  ],
  verifyPassword: [
    {required: true, validator: verifyPasswordValidator, trigger: 'blur'},
  ],
  deptId: [
    {required: true, message: '请选择部门', trigger: 'change'},
  ],
  roles: [
    {required: true, validator: roleValidator, trigger: 'change'},
  ],
  status: [
    {required: true, message: '请选择状态', trigger: 'change'},
  ],
});

// 保存
const saveLoading = ref(false);
const save = async (formEl: FormInstance) => {
  if (!formEl) return
  await formEl.validate((valid: boolean) => {
    if (valid) {
      props.form.roleIds = selectedRoles.value.join(",");
      saveLoading.value = true;
      editUserApi(props.form).then(res => {
        saveLoading.value = false;
        if (res.data.code === 200) {
          ElMessage.success({message: res.data.data, showClose: true});
          emit('save', true);
        } else {
          ElMessage.error({message: res.data.data, showClose: true});
        }
      })
    }
  })
}

onMounted(() => {
  getRoleList();
})
</script>

<template>
  <div class="form-div">
    <el-form v-loading="saveLoading" :model="form" :rules="rules" ref="formRef" label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名"/>
      </el-form-item>
      <el-form-item label="用户名" prop="account">
        <el-input v-model="form.account" :disabled="edit" placeholder="请输入用户名">
        </el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="!edit">
        <el-input type="password" v-model="form.password" placeholder="请输入密码"/>
      </el-form-item>
      <el-form-item label="确认密码" prop="verifyPassword" v-if="!edit">
        <el-input type="password" v-model="verifyPassword" placeholder="确认密码"/>
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <el-select v-model="form.deptId" placeholder="请选择部门">
          <el-option v-for="item in deptList" :key="item.id" :label="item.deptName" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="角色" prop="roles">
        <el-select multiple v-model="selectedRoles" placeholder="请选择角色" :value="echoRoles">
          <el-option v-for="item in roleList" :key="item.id" :label="item.roleName" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :value="1">启用</el-radio>
          <el-radio :value="2">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save(formRef)">保存</el-button>
        <el-button @click="$emit('close')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>

</style>
